home *** CD-ROM | disk | FTP | other *** search
/ PC-Blue - MS DOS Public Domain Library / PC-Blue MS-DOS Public Domain Library - NYACC.iso / vol086 / roff45.c < prev    next >
Encoding:
C/C++ Source or Header  |  1986-12-15  |  7.5 KB  |  266 lines

  1. /********************************************************/
  2. /*                            */
  3. /*            ROFF4, Version 1.50            */
  4. /*                            */
  5. /* (C) 1983 by    Ernest E. Bergmann            */
  6. /*        Physics, Building #16            */
  7. /*        Lehigh Univerisity            */
  8. /*        Bethlehem, Pa. 18015            */
  9. /*                            */
  10. /* Permission is hereby granted for all commercial and    */
  11. /* non-commercial reproduction and distribution of this    */
  12. /* material provided this notice is included.        */
  13. /*                            */
  14. /********************************************************/
  15. /*June 27, 1983*/
  16. /********************************************************/
  17. /* March 1984  DeSmet C version */
  18. /* Henry Harpending*/
  19. /* Anthropology Department*/
  20. /* University of New Mexico*/
  21. /* Albuquerque, NM  87131*/
  22. /*   no restrictions */
  23. /*********************************************************/
  24. #include "a:roff4.h"
  25. /****************************************/
  26. puttl3(s1,s2,s3,pageno)
  27. char *s1,*s2,*s3;
  28. int pageno;        /*put out three part title, none
  29.             containing '\n', with
  30.             optional page numbering; aligning
  31.             with page margins,0 & OWVAL;*/
  32. {int size1,gap1,size2,gap2,size3,gaps,remain;
  33.     size1 =strln3(s1,FALSE,pageno);
  34.     OUTTOP=LTOP;OUTBOT=LBOT;
  35.     size2 =strln3(s2,FALSE,pageno);
  36.     if(LTOP<OUTTOP) OUTTOP=LTOP;
  37.     size3 =strln3(s3,FALSE,pageno);
  38.     if(LTOP<OUTTOP) OUTTOP=LTOP;
  39.     gaps = max(0,OWVAL-size1-size2-size3);
  40.     gap1 = gaps/2;
  41.     gap2 = gaps-gap1;
  42.     remain=OWVAL;
  43.     if(size1<=remain){puttl(s1,pageno);remain-=size1;}
  44.     if(gap1<remain){blanks(gap1);remain-=gap1;}
  45.     if(size2<=remain){puttl(s2,pageno);remain-=size2;}
  46.     if(gap2<remain){blanks(gap2);remain-=gap2;}
  47.     if(size3<=remain){puttl(s3,pageno);remain-=size3;}
  48.     printout();
  49.     cputc('\r',ofp);
  50. }
  51. /****************************************/
  52. blanks(i)
  53. int i;    /*sends i blanks to OUTBUF2*/
  54. {    if(i<0) return;
  55.     for( ; i ; i--) putout( BLANK );
  56. }
  57. /****************************************/
  58. gettl3(sl,ttl1,ttl2,ttl3)
  59. char *sl,ttl1[MAXLINE],**ttl2,**ttl3;
  60.     /* Gets from source line three part title that it
  61.     transfers to buffer delineated by ttl1 which has
  62.     capacity for all three strings; none of the strings
  63.     will contain '\n' */
  64. {char c, cc, *dp;
  65. if DEBUG fprintf(stderr,"\n\nGETTL3 sl =<%s>",sl);
  66.     /*pass over command*/
  67.     for(c=*sl;c!=' '&&c!='\n'&&c!='\t';sl++)c=*sl;
  68.     /*advance to first non-blank or '\n' */
  69.     for(;c==' '||c=='\t';sl++)c=*sl;
  70.     /*advance beyond delim. if present:*/
  71.     if(c!='\n'&&!c) sl++;
  72.     dp=ttl1;
  73.     transfer(&sl,&dp,c);
  74.     *ttl2=dp;
  75.     transfer(&sl,&dp,c);
  76.     *ttl3=dp;
  77.     transfer(&sl,&dp,c);
  78. if DEBUG
  79.    fprintf(stderr,"\ndelim=<%c>\nT1=<%s>\nT2=<%s>\nT3=<%s>;\n",
  80.                   c,ttl1,*ttl2,*ttl3);
  81. }
  82. /****************************************/
  83. transfer(s,d,c)
  84. char c;        /*terminal character*/
  85. char **s;    /*source string*/
  86. char **d;    /*destination string*/
  87. /* Copy string from source to destination.  Original delim.
  88. can be \0, \n, or char.  The pointer to the source is updated
  89. to point at the \0, \n, or past char.  In destination, delim.
  90. is always replaced by \0.  The destination pointer always
  91. points past this \0. */
  92. {char a;
  93.     a=**s;
  94.     while(a!=c && a!='\n' && a)
  95.         {**d = a;  (*d)++;
  96.         (*s)++;  a=**s;
  97.         }
  98.     **d='\0';
  99.     (*d)++;
  100.     if(a!='\n' && a!='\0') (*s)++;
  101. }
  102. /**********************************************************
  103.         centers a line of text
  104. **********************************************************/
  105. center (line)
  106. char *line;
  107. {TIVAL = max(( RMVAL+TIVAL-strln3(line,FALSE,1))/2, 0 );
  108. OUTTOP=LTOP;OUTBOT=LBOT;
  109. return;
  110. }
  111. /************************************************************
  112. Revised April 24,83.Transfers next word from in to out.  Scans
  113. off leading white space from in.  If there is no word, returns
  114. FALSE.  Otherwise, input is truncated on left of word which is
  115. transfered to out without leading or trailling blanks.
  116. WE_HAVE_A_WORD will be returned.  If the transfered word
  117. terminates a sentence then SENTENCE is set to 1, otherwise it
  118. is reset to FALSE.
  119. **************************************************************/
  120. int getwrd (in,  out )
  121. char *in, *out;
  122. {char *pin,*pout,c,cm,cp;
  123.     skip_blanks(in);
  124.     replace_char(in,TAB,BLANK);
  125.     pin=in;
  126.     pout=out;
  127.     c=*pin;
  128.     SENTENCE=FALSE;
  129.     if(c==NEWLINE || c=='\0')
  130.         {*out='\0';
  131.         if DEBUG fprintf(stderr,"\ngetwrd=<%s>",out);
  132.         return(FALSE);
  133.         }
  134.     while(c!=BLANK && c!=NEWLINE && c)
  135.         {*(pout++)=c;
  136.         *pin=BLANK;
  137.         c=*(++pin);
  138.         }
  139.     *pout='\0';    /*terminate out string*/
  140.     cm=*(pout-1);
  141.     cp=*(pin+1);
  142.     switch (cm)
  143.         {case ':' :
  144.         case ';' :
  145.         case '?' :
  146.         case '!' :
  147.             SENTENCE=1;
  148.             break;
  149.         case '.' :
  150.             if(cp==BLANK||cp==NEWLINE||c==NEWLINE)
  151.                 SENTENCE=1;
  152.         }
  153.     if DEBUG fprintf("\ngetwrd=<%s>",out);
  154.     return(WE_HAVE_A_WORD);
  155. }
  156. /*******************************************************
  157. Truncates white-space characters at the end of a string.
  158. ********************************************************/
  159. trunc_bl (string)
  160. char *string;
  161. {char *ptr;
  162. int k;
  163. k = strlen (string);
  164. ptr = &string[ k-1 ];    /* char before terminating nul */
  165. while (*ptr==BLANK || *ptr==TAB || *ptr==NEWLINE)    
  166.     *ptr--  = '\0';
  167. }
  168. /*********************************************
  169.     distribute words evenly across a line
  170. **********************************************/
  171. spread ( line, nextra, no_words)
  172. char *line;
  173. int nextra;    /* no. extra places left in line */
  174. int no_words;   /* no. words in the line         */
  175. {int i, j, nblanks, nholes;
  176. if DEBUG fprintf(stderr,"spread:line=<%s>,\nnextra=%d, no_words=%d\n",
  177.                           line,      nextra,    no_words   );
  178. if (nextra <= 0 || no_words <= 1)    return;
  179. DIR = !(DIR);
  180. nholes = no_words - 1;
  181. trunc_bl (line);
  182. i = strlen(line) - 1 ; /* last character of string */
  183. j = min(MAXLINE-2,i+nextra);    /* last  position in output */
  184. line[j+1] = '\0';
  185. for ( ; i<j ; i--, j-- )
  186.       { line[j] = line[i];
  187.     if ( line[i] == BLANK)
  188.           { if (DIR == 0) nblanks=(nextra-1)/nholes+1;
  189.         else        nblanks = nextra/nholes;
  190.         nextra = nextra - nblanks;
  191.         nholes = nholes - 1;
  192.         for ( ; nblanks > 0;  nblanks-- )
  193.             line[--j] = BLANK;
  194.           }
  195.       }
  196. }
  197. /************************************************
  198. place portion of title line with optional page no. in OUTBUF2
  199. *************************************************/
  200. puttl ( str, num )
  201. char *str;
  202. int num;
  203. {int i;
  204. char c;
  205. for (i=0;c= *str; str++)
  206.     {if ( c != NUMSIGN ) putout(c);
  207.     else putnum(num);
  208.     }
  209. }
  210. /*******************************************
  211.     put out num to OUTBUF2 (conversion)
  212. ********************************************/
  213. putnum ( num, w )
  214. int num;
  215. {int i, nd;
  216. char chars[10];
  217. nd = itoc ( num, chars, 10 );
  218. for ( i=0;i<nd; i++) putout(chars[i]);
  219. }
  220. /************************************************
  221.     convert int num to char string in numstr
  222. *************************************************/
  223. itoc ( num, numstr, size )
  224. int num;
  225. char *numstr;
  226. int size;    /* largest size of numstr */
  227. {int absnum, i, j, k, d;
  228. absnum = abs (num);
  229. numstr[0] = '\0';
  230. i = 0;
  231. do      { i++;
  232.     d = absnum % 10;
  233.     numstr[i] = d + '0';
  234.     absnum = absnum/10;
  235.     }
  236. while ( absnum != 0 && i<size );
  237. if ( num < 0 && i<size )
  238.       { i++;
  239.     numstr[i] = '-';
  240.       }
  241. for( j=0; j<i; j++ )
  242.       { k = numstr[i];
  243.     numstr[i] = numstr[j];
  244.     numstr[j] = k;
  245.     i--;
  246.       }
  247. return ( strlen(numstr) );
  248. }
  249. /****************************************/
  250. putout(c)    /*places c in OUTBUF2[]*/
  251. char c;
  252. {if(c==SCVAL) c= BLANK;
  253. if(c==NEWLINE)c='\0';
  254. OUTBUF2[BPOS++]=c;
  255. OUTBUF2[BPOS]='\0';/*safty net*/
  256. }
  257. /************************************
  258.     replace c1 in string with c2
  259. *************************************/
  260. replace_char (string, c1, c2)
  261. char *string, c1, c2;
  262. {int i;
  263. for (i=0; string[i]; i++)
  264.     if (string[i] == c1)    string[i] = c2;
  265. }
  266.